﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Globalization
Imports System.Runtime.CompilerServices
Imports System.Threading
Imports System.Workflow.ComponentModel
Imports System.Workflow.Runtime

Namespace System.Workflow.Activities
    Friend NotInheritable Class CorrelationTokenTypeConverter
        Inherits ExpandableObjectConverter
        ' Methods
        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            Return (sourceType Is GetType(String))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            Return (destinationType Is GetType(String))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
            Dim obj2 As Object = Nothing
            Dim str As String = [TryCast](value,String)
            If Not String.IsNullOrEmpty(str) Then
                Dim obj3 As Object
                For Each obj3 In Me.GetStandardValues(context)
                    Dim token As CorrelationToken = [TryCast](obj3,CorrelationToken)
                    If ((Not token Is Nothing) AndAlso (token.Name = str)) Then
                        obj2 = token
                        Exit For
                    End If
                Next
                If (obj2 Is Nothing) Then
                    obj2 = New CorrelationToken(str)
                End If
            End If
            Return obj2
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            Dim name As Object = Nothing
            Dim token As CorrelationToken = [TryCast](value,CorrelationToken)
            If ((destinationType Is GetType(String)) AndAlso (Not token Is Nothing)) Then
                name = token.Name
            End If
            Return name
        End Function

        Private Function GetContainedActivities(ByVal activity As CompositeActivity) As IEnumerable
            Return New <GetContainedActivities>d__c(-2) { _
                .<>4__this = Me, _
                .<>3__activity = activity _
            }
        End Function

        Private Function GetPreceedingActivities(ByVal startActivity As Activity) As IEnumerable
            Return New <GetPreceedingActivities>d__0(-2) { _
                .<>4__this = Me, _
                .<>3__startActivity = startActivity _
            }
        End Function

        Public Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
            Dim list As New ArrayList(MyBase.GetProperties(context, value, attributes))
            Return New PropertyDescriptorCollection(DirectCast(list.ToArray(GetType(PropertyDescriptor)), PropertyDescriptor()))
        End Function

        Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
            Dim values As New ArrayList
            Dim instance As Activity = TryCast(context.Instance,Activity)
            If (Not instance Is Nothing) Then
                Dim activity2 As Activity
                For Each activity2 In Me.GetPreceedingActivities(instance)
                    Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(activity2).Item("CorrelationToken")
                    If (Not descriptor Is Nothing) Then
                        Dim item As CorrelationToken = TryCast(descriptor.GetValue(activity2),CorrelationToken)
                        If ((Not item Is Nothing) AndAlso Not values.Contains(item)) Then
                            values.Add(item)
                        End If
                    End If
                Next
            End If
            Return New StandardValuesCollection(values)
        End Function

        Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
            Return False
        End Function

        Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function


        ' Nested Types
        <CompilerGenerated> _
        Private NotInheritable Class <GetContainedActivities>d__c
            Implements IEnumerable(Of Object), IEnumerable, IEnumerator(Of Object), IEnumerator, IDisposable
            ' Methods
            <DebuggerHidden> _
            Public Sub New(ByVal <>1__state As Integer)
                Me.<>1__state = <>1__state
                Me.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId
            End Sub

            Private Sub <>m__Finally10()
                Me.<>1__state = -1
                If (Not Me.<>7__wrapf Is Nothing) Then
                    Me.<>7__wrapf.Dispose
                End If
            End Sub

            Private Sub <>m__Finally13()
                Me.<>1__state = 1
                Me.<>7__wrap12 = TryCast(Me.<>7__wrap11,IDisposable)
                If (Not Me.<>7__wrap12 Is Nothing) Then
                    Me.<>7__wrap12.Dispose
                End If
            End Sub

            Private Function MoveNext() As Boolean Implements IEnumerator.MoveNext
                Dim flag As Boolean
                Try 
                    Select Case Me.<>1__state
                        Case 0
                            Me.<>1__state = -1
                            If Not Me.activity.Enabled Then
                                goto Label_0141
                            End If
                            Me.<>7__wrapf = Me.activity.Activities.GetEnumerator
                            Me.<>1__state = 1
                            goto Label_012B
                        Case 3
                            goto Label_00E4
                        Case 4
                            Me.<>1__state = 1
                            goto Label_012B
                        Case Else
                            goto Label_0141
                    End Select
                Label_005F:
                    Me.<containedActivity>5__d = Me.<>7__wrapf.Current
                    If TypeOf Me.<containedActivity>5__d Is CompositeActivity Then
                        Me.<>7__wrap11 = Me.<>4__this.GetContainedActivities(DirectCast(Me.<containedActivity>5__d, CompositeActivity)).GetEnumerator
                        Me.<>1__state = 2
                        Do While Me.<>7__wrap11.MoveNext
                            Me.<nestedActivity>5__e = DirectCast(Me.<>7__wrap11.Current, Activity)
                            If Not Me.<nestedActivity>5__e.Enabled Then
                                Continue Do
                            End If
                            Me.<>2__current = Me.<nestedActivity>5__e
                            Me.<>1__state = 3
                            Return True
                        Label_00E4:
                            Me.<>1__state = 2
                        Loop
                        Me.<>m__Finally13
                    ElseIf Me.<containedActivity>5__d.Enabled Then
                        Me.<>2__current = Me.<containedActivity>5__d
                        Me.<>1__state = 4
                        Return True
                    End If
                Label_012B:
                    If Me.<>7__wrapf.MoveNext Then
                        goto Label_005F
                    End If
                    Me.<>m__Finally10
                Label_0141:
                    flag = False
                Fault
                    Me.System.IDisposable.Dispose
                End Try
                Return flag
            End Function

            <DebuggerHidden> _
            Private Function System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator() As IEnumerator(Of Object) Implements IEnumerable(Of Object).GetEnumerator
                Dim _c As <GetContainedActivities>d__c
                If ((Thread.CurrentThread.ManagedThreadId = Me.<>l__initialThreadId) AndAlso (Me.<>1__state = -2)) Then
                    Me.<>1__state = 0
                    _c = Me
                Else
                    _c = New <GetContainedActivities>d__c(0) { _
                        .<>4__this = Me.<>4__this _
                    }
                End If
                _c.activity = Me.<>3__activity
                Return _c
            End Function

            <DebuggerHidden> _
            Private Function System.Collections.IEnumerable.GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
                Return Me.System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator
            End Function

            <DebuggerHidden> _
            Private Sub System.Collections.IEnumerator.Reset() Implements IEnumerator.Reset
                Throw New NotSupportedException
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                Select Case Me.<>1__state
                    Case 1, 2, 3, 4
                        Try 
                            Select Case Me.<>1__state
                                Case 2, 3
                                    Try 
                                    Finally
                                        Me.<>m__Finally13
                                    End Try
                                    Exit Select
                            End Select
                        Finally
                            Me.<>m__Finally10
                        End Try
                        Exit Select
                    Case Else
                        Return
                End Select
            End Sub


            ' Properties
            Private ReadOnly Property System.Collections.Generic.IEnumerator<System.Object>.Current As Object
                <DebuggerHidden> _
                Get
                    Return Me.<>2__current
                End Get
            End Property

            Private ReadOnly Property System.Collections.IEnumerator.Current As Object
                <DebuggerHidden> _
                Get
                    Return Me.<>2__current
                End Get
            End Property


            ' Fields
            Private <>1__state As Integer
            Private <>2__current As Object
            Public <>3__activity As CompositeActivity
            Public <>4__this As CorrelationTokenTypeConverter
            Public <>7__wrap11 As IEnumerator
            Public <>7__wrap12 As IDisposable
            Public <>7__wrapf As IEnumerator(Of Activity)
            Private <>l__initialThreadId As Integer
            Public <containedActivity>5__d As Activity
            Public <nestedActivity>5__e As Activity
            Public activity As CompositeActivity
        End Class

        <CompilerGenerated> _
        Private NotInheritable Class <GetPreceedingActivities>d__0
            Implements IEnumerable(Of Object), IEnumerable, IEnumerator(Of Object), IEnumerator, IDisposable
            ' Methods
            <DebuggerHidden> _
            Public Sub New(ByVal <>1__state As Integer)
                Me.<>1__state = <>1__state
                Me.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId
            End Sub

            Private Sub <>m__Finally6()
                Me.<>1__state = -1
                If (Not Me.<>7__wrap5 Is Nothing) Then
                    Me.<>7__wrap5.Dispose
                End If
            End Sub

            Private Sub <>m__Finally9()
                Me.<>1__state = 1
                Me.<>7__wrap8 = TryCast(Me.<>7__wrap7,IDisposable)
                If (Not Me.<>7__wrap8 Is Nothing) Then
                    Me.<>7__wrap8.Dispose
                End If
            End Sub

            Private Function MoveNext() As Boolean Implements IEnumerator.MoveNext
                Dim flag As Boolean
                Try 
                    Select Case Me.<>1__state
                        Case 0
                            Me.<>1__state = -1
                            Me.<currentActivity>5__1 = Nothing
                            Me.<activityStack>5__2 = New Stack(Of Activity)
                            Me.<activityStack>5__2.Push(Me.startActivity)
                            goto Label_018B
                        Case 3
                            goto Label_0125
                        Case 4
                            goto Label_0158
                        Case Else
                            goto Label_01A4
                    End Select
                Label_0055:
                    If (Not Me.<currentActivity>5__1.Parent Is Nothing) Then
                        Me.<>7__wrap5 = Me.<currentActivity>5__1.Parent.Activities.GetEnumerator
                        Me.<>1__state = 1
                        Do While Me.<>7__wrap5.MoveNext
                            Me.<siblingActivity>5__3 = Me.<>7__wrap5.Current
                            If (Me.<siblingActivity>5__3 Is Me.<currentActivity>5__1) Then
                                Exit Do
                            End If
                            If Not Me.<siblingActivity>5__3.Enabled Then
                                Continue Do
                            End If
                            If TypeOf Me.<siblingActivity>5__3 Is CompositeActivity Then
                                Me.<>7__wrap7 = Me.<>4__this.GetContainedActivities(DirectCast(Me.<siblingActivity>5__3, CompositeActivity)).GetEnumerator
                                Me.<>1__state = 2
                                Do While Me.<>7__wrap7.MoveNext
                                    Me.<containedActivity>5__4 = DirectCast(Me.<>7__wrap7.Current, Activity)
                                    Me.<>2__current = Me.<containedActivity>5__4
                                    Me.<>1__state = 3
                                    Return True
                                Label_0125:
                                    Me.<>1__state = 2
                                Loop
                                Me.<>m__Finally9
                                Continue Do
                            End If
                            Me.<>2__current = Me.<siblingActivity>5__3
                            Me.<>1__state = 4
                            Return True
                        Label_0158:
                            Me.<>1__state = 1
                        Loop
                        Me.<>m__Finally6
                    End If
                    Me.<activityStack>5__2.Push(Me.<currentActivity>5__1.Parent)
                Label_018B:
                    If (Not Me.<currentActivity>5__1 = Me.<activityStack>5__2.Pop Is Nothing) Then
                        goto Label_0055
                    End If
                Label_01A4:
                    flag = False
                Fault
                    Me.System.IDisposable.Dispose
                End Try
                Return flag
            End Function

            <DebuggerHidden> _
            Private Function System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator() As IEnumerator(Of Object) Implements IEnumerable(Of Object).GetEnumerator
                Dim d__ As <GetPreceedingActivities>d__0
                If ((Thread.CurrentThread.ManagedThreadId = Me.<>l__initialThreadId) AndAlso (Me.<>1__state = -2)) Then
                    Me.<>1__state = 0
                    d__ = Me
                Else
                    d__ = New <GetPreceedingActivities>d__0(0) { _
                        .<>4__this = Me.<>4__this _
                    }
                End If
                d__.startActivity = Me.<>3__startActivity
                Return d__
            End Function

            <DebuggerHidden> _
            Private Function System.Collections.IEnumerable.GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
                Return Me.System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator
            End Function

            <DebuggerHidden> _
            Private Sub System.Collections.IEnumerator.Reset() Implements IEnumerator.Reset
                Throw New NotSupportedException
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                Select Case Me.<>1__state
                    Case 1, 2, 3, 4
                        Try 
                            Select Case Me.<>1__state
                                Case 2, 3
                                    Try 
                                    Finally
                                        Me.<>m__Finally9
                                    End Try
                                    Exit Select
                            End Select
                        Finally
                            Me.<>m__Finally6
                        End Try
                        Exit Select
                    Case Else
                        Return
                End Select
            End Sub


            ' Properties
            Private ReadOnly Property System.Collections.Generic.IEnumerator<System.Object>.Current As Object
                <DebuggerHidden> _
                Get
                    Return Me.<>2__current
                End Get
            End Property

            Private ReadOnly Property System.Collections.IEnumerator.Current As Object
                <DebuggerHidden> _
                Get
                    Return Me.<>2__current
                End Get
            End Property


            ' Fields
            Private <>1__state As Integer
            Private <>2__current As Object
            Public <>3__startActivity As Activity
            Public <>4__this As CorrelationTokenTypeConverter
            Public <>7__wrap5 As IEnumerator(Of Activity)
            Public <>7__wrap7 As IEnumerator
            Public <>7__wrap8 As IDisposable
            Private <>l__initialThreadId As Integer
            Public <activityStack>5__2 As Stack(Of Activity)
            Public <containedActivity>5__4 As Activity
            Public <currentActivity>5__1 As Activity
            Public <siblingActivity>5__3 As Activity
            Public startActivity As Activity
        End Class
    End Class
End Namespace

